/*
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at 
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
using System;
using java = biz.ritter.javapi; 

namespace biz.ritter.javapi.io
{
	/**
 * Wraps an existing {@link InputStream} and counts the line terminators
 * encountered while reading the data. Line numbering starts at 0. Recognized
 * line terminator sequences are {@code '\r'}, {@code '\n'} and {@code "\r\n"}.
 * When using {@code read}, line terminator sequences are always translated into
 * {@code '\n'}.
 * 
 * @deprecated Use {@link LineNumberReader}
 */
	[Obsolete]
	public class LineNumberInputStream : FilterInputStream
	{

		private int lineNumber;
		private int markedLineNumber = -1;
		private int lastChar = -1;
		private int markedLastChar;

		/**
     * Constructs a new {@code LineNumberInputStream} on the {@link InputStream}
     * {@code in}. Line numbers are counted for all data read from this stream.
     * 
     * @param in
     *            The non-null input stream to count line numbers.
     */
		public LineNumberInputStream (InputStream inJ) : base(inJ)
		{
        
		}

		/**
     * Returns the number of bytes that are available before this stream will
     * block.
     * <p>
     * Note: The source stream may just be a sequence of {@code "\r\n"} bytes
     * which are converted into {@code '\n'} by this stream. Therefore,
     * {@code available} returns only {@code in.available() / 2} bytes as
     * result.
     *
     * @return the guaranteed number of bytes available before blocking.
     * @throws IOException
     *             if an error occurs in this stream.
     */
    
		public override int available ()
		{// throws IOException {
			return inJ.available () / 2 + (lastChar == -1 ? 0 : 1);
		}

		/**
     * Returns the current line number for this stream. Numbering starts at 0.
     * 
     * @return the current line number.
     */
		public int getLineNumber ()
		{
			return lineNumber;
		}

		/**
     * Sets a mark position in this stream. The parameter {@code readlimit}
     * indicates how many bytes can be read before the mark is invalidated.
     * Sending {@code reset()} will reposition this stream back to the marked
     * position, provided that {@code readlimit} has not been surpassed.
     * The line number count will also be reset to the last marked
     * line number count.
     * <p>
     * This implementation sets a mark in the filtered stream.
     *
     * @param readlimit
     *            the number of bytes that can be read from this stream before
     *            the mark is invalidated.
     * @see #markSupported()
     * @see #reset()
     */
    
		public override void mark (int readlimit)
		{
			inJ.mark (readlimit);
			markedLineNumber = lineNumber;
			markedLastChar = lastChar;
		}

		/**
     * Reads a single byte from the filtered stream and returns it as an integer
     * in the range from 0 to 255. Returns -1 if the end of this stream has been
     * reached.
     * <p>
     * The line number count is incremented if a line terminator is encountered.
     * Recognized line terminator sequences are {@code '\r'}, {@code '\n'} and
     * {@code "\r\n"}. Line terminator sequences are always translated into
     * {@code '\n'}.
     *
     * @return the byte read or -1 if the end of the filtered stream has been
     *         reached.
     * @throws IOException
     *             if the stream is closed or another IOException occurs.
     */
		public override int read ()
		{//throws IOException {
			int currentChar = lastChar;
			if (currentChar == -1) {
				currentChar = inJ.read ();
			} else {
				lastChar = -1;
			}
			switch (currentChar) {
			case '\r':
				currentChar = '\n';
				lastChar = inJ.read ();
				if (lastChar == '\n') {
					lastChar = -1;
				}
				lineNumber++;
				break;
			// fall through
			case '\n':
				lineNumber++;
				break;
			default:
				break;
			}
			return currentChar;
		}

		/**
     * Reads at most {@code length} bytes from the filtered stream and stores
     * them in the byte array {@code buffer} starting at {@code offset}.
     * Returns the number of bytes actually read or -1 if no bytes have been
     * read and the end of this stream has been reached.
     * <p>
     * The line number count is incremented if a line terminator is encountered.
     * Recognized line terminator sequences are {@code '\r'}, {@code '\n'} and
     * {@code "\r\n"}. Line terminator sequences are always translated into
     * {@code '\n'}.
     *
     * @param buffer
     *            the array in which to store the bytes read.
     * @param offset
     *            the initial position in {@code buffer} to store the bytes read
     *            from this stream.
     * @param length
     *            the maximum number of bytes to store in {@code buffer}.
     * @return the number of bytes actually read or -1 if the end of the
     *         filtered stream has been reached while reading.
     * @throws IndexOutOfBoundsException
     *             if {@code offset < 0} or {@code length < 0}, or if
     *             {@code offset + length} is greater than the length of
     *             {@code buffer}.
     * @throws IOException
     *             if this stream is closed or another IOException occurs.
     * @throws NullPointerException
     *             if {@code buffer} is {@code null}.
     */
    
		public override int read (byte[] buffer, int offset, int length)
		{//throws IOException {
			// Force buffer null check first!
			if (offset > buffer.Length || offset < 0) {
				// luni.12=Offset out of bounds \: {0}
				throw new java.lang.ArrayIndexOutOfBoundsException ("Offset out of bounds : " + offset); //$NON-NLS-1$
			} 
			if (length < 0 || length > buffer.Length - offset) {
				// luni.18=Length out of bounds \: {0}
				throw new java.lang.ArrayIndexOutOfBoundsException ("Length out of bounds : " + length); //$NON-NLS-1$
			}

			for (int i = 0; i < length; i++) {
				int currentChar;
				try {
					currentChar = read ();
				} catch (IOException e) {
					if (i != 0) {
						return i;
					}
					throw e;
				}
				if (currentChar == -1) {
					return i == 0 ? -1 : i;
				}
				buffer [offset + i] = (byte)currentChar;
			}
			return length;
		}

		/**
     * Resets this stream to the last marked location. It also resets the line
     * count to what is was when this stream was marked.
     * 
     * @throws IOException
     *             if this stream is already closed, no mark has been set or the
     *             mark is no longer valid because more than {@code readlimit}
     *             bytes have been read since setting the mark.
     * @see #mark(int)
     * @see #markSupported()
     */
    
		public override void reset ()
		{//throws IOException {
			inJ.reset ();
			lineNumber = markedLineNumber;
			lastChar = markedLastChar;
		}

		/**
     * Sets the line number of this stream to the specified
     * {@code lineNumber}. Note that this may have side effects on the
     * line number associated with the last marked position.
     * 
     * @param lineNumber
     *            the new lineNumber value.
     * @see #mark(int)
     * @see #reset()
     */
		public void setLineNumber (int lineNumber)
		{
			this.lineNumber = lineNumber;
		}

		/**
     * Skips {@code count} number of bytes in this stream. Subsequent
     * {@code read()}'s will not return these bytes unless {@code reset()} is
     * used. This implementation skips {@code count} number of bytes in the
     * filtered stream and increments the line number count whenever line
     * terminator sequences are skipped.
     * 
     * @param count
     *            the number of bytes to skip.
     * @return the number of bytes actually skipped.
     * @throws IOException
     *             if this stream is closed or another IOException occurs.
     * @see #mark(int)
     * @see #read()
     * @see #reset()
     */
    
		public override long skip (long count)
		{//throws IOException {
			if (count <= 0) {
				return 0;
			}
			for (int i = 0; i < count; i++) {
				int currentChar = read ();
				if (currentChar == -1) {
					return i;
				}
			}
			return count;
		}
	}
}